<?php

/**
 * This is the model class for table "metas".
 *
 * The followings are the available columns in table 'metas':
 * @property string $mid
 * @property string $name
 * @property string $slug
 * @property string $type
 * @property string $description
 * @property string $count
 * @property string $order
 */
class Metas extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Metas the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{metas}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type', 'required'),
			array('name, slug, description', 'length', 'max'=>200),
			array('type', 'length', 'max'=>32),
			array('count, order', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('mid, name, slug, type, description, count, order', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'mid' => 'Mid',
			'name' => 'Name',
			'slug' => 'Slug',
			'type' => 'Type',
			'description' => 'Description',
			'count' => 'Count',
			'order' => 'Order',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('mid',$this->mid,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('count',$this->count,true);
		$criteria->compare('order',$this->order,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	private static $_items=array();
	public static function items($type='category'){
		if(!isset(self::$_items[$type])){
			self::$_items[$type]=array();
			$criteria=new CDbCriteria;
			$criteria->addColumnCondition(array('type'=>$type));
			$models=self::model()->findAll($criteria);		
			foreach($models as $model){
				self::$_items[$type][$model->mid]=$model->name;
			}
		}
		return self::$_items[$type];
	}


	public static function str2Array($str){
		$mids=array();
		$arr=explode(',',$str);
		foreach($arr as $v){
			if($v!=''){
				$criteria=new CDbCriteria;
				$criteria->addColumnCondition(array('name'=>$v,'type'=>'tag'));
				$model=self::model()->find($criteria);		
				if(isset($model)){
					$mids[]=$model->mid;
				}else{
					$metas=new Metas;
					$metas->name=$v;
					$metas->type='tag';
					if($metas->save()){
						$mids[]=$metas->mid;
					}
				}
			}
		}
		return $mids;
	}
}
